<template>
  <div class="parameters-settings">
    <v-container>
      <!-- 询标限制设置 -->
      <v-card class="mb-6">
        <v-card-title class="d-flex align-center">
          <v-icon color="primary" class="mr-2">mdi-cog</v-icon>
          询标限制设置
        </v-card-title>
        <v-card-text>
          <v-form ref="inquiryForm" v-model="inquiryFormValid">
            <v-row>
              <v-col cols="12" md="6">
                <v-text-field
                  v-model="inquirySettings.maxInquiriesPerDay"
                  label="每日最大询标次数"
                  type="number"
                  :rules="[rules.required, rules.positive]"
                  outlined
                  dense
                ></v-text-field>
              </v-col>
              <v-col cols="12" md="6">
                <v-text-field
                  v-model="inquirySettings.maxInquiriesPerProject"
                  label="每个项目最大询标次数"
                  type="number"
                  :rules="[rules.required, rules.positive]"
                  outlined
                  dense
                ></v-text-field>
              </v-col>
              <v-col cols="12" md="6">
                <v-text-field
                  v-model="inquirySettings.replyDeadlineHours"
                  label="回复截止时间（小时）"
                  type="number"
                  :rules="[rules.required, rules.positive]"
                  outlined
                  dense
                ></v-text-field>
              </v-col>
              <v-col cols="12" md="6">
                <v-text-field
                  v-model="inquirySettings.maxAttachments"
                  label="最大附件数量"
                  type="number"
                  :rules="[rules.required, rules.positive]"
                  outlined
                  dense
                ></v-text-field>
              </v-col>
              <v-col cols="12" md="6">
                <v-text-field
                  v-model="inquirySettings.maxAttachmentSize"
                  label="单个附件最大大小（MB）"
                  type="number"
                  :rules="[rules.required, rules.positive]"
                  outlined
                  dense
                ></v-text-field>
              </v-col>
            </v-row>
          </v-form>
        </v-card-text>
        <v-card-actions>
          <v-spacer></v-spacer>
          <v-btn
            color="primary"
            :loading="savingInquiry"
            :disabled="!inquiryFormValid"
            @click="saveInquirySettings"
          >
            保存设置
          </v-btn>
        </v-card-actions>
      </v-card>

      <!-- 短信通知设置 -->
      <v-card>
        <v-card-title class="d-flex align-center">
          <v-icon color="primary" class="mr-2">mdi-message-text</v-icon>
          短信通知设置
        </v-card-title>
        <v-card-text>
          <v-form ref="smsForm" v-model="smsFormValid">
            <v-row>
              <v-col cols="12" md="6">
                <v-switch
                  v-model="smsSettings.enableSms"
                  label="启用短信通知"
                  color="primary"
                  hide-details
                ></v-switch>
              </v-col>
              <v-col cols="12" md="6">
                <v-switch
                  v-model="smsSettings.enableVoice"
                  label="启用语音通知"
                  color="primary"
                  hide-details
                ></v-switch>
              </v-col>
              <v-col cols="12" md="6">
                <v-text-field
                  v-model="smsSettings.smsRetryCount"
                  label="短信重试次数"
                  type="number"
                  :rules="[rules.required, rules.positive]"
                  outlined
                  dense
                ></v-text-field>
              </v-col>
              <v-col cols="12" md="6">
                <v-text-field
                  v-model="smsSettings.voiceRetryCount"
                  label="语音重试次数"
                  type="number"
                  :rules="[rules.required, rules.positive]"
                  outlined
                  dense
                ></v-text-field>
              </v-col>
              <v-col cols="12" md="6">
                <v-text-field
                  v-model="smsSettings.retryInterval"
                  label="重试间隔（分钟）"
                  type="number"
                  :rules="[rules.required, rules.positive]"
                  outlined
                  dense
                ></v-text-field>
              </v-col>
            </v-row>
          </v-form>
        </v-card-text>
        <v-card-actions>
          <v-spacer></v-spacer>
          <v-btn
            color="primary"
            :loading="savingSms"
            :disabled="!smsFormValid"
            @click="saveSmsSettings"
          >
            保存设置
          </v-btn>
        </v-card-actions>
      </v-card>
    </v-container>
  </div>
</template>

<script>
export default {
  name: 'ParametersSettings',
  data() {
    return {
      inquiryFormValid: false,
      smsFormValid: false,
      savingInquiry: false,
      savingSms: false,
      rules: {
        required: v => !!v || '此项为必填项',
        positive: v => Number(v) > 0 || '数值必须大于0'
      },
      inquirySettings: {
        maxInquiriesPerDay: 10,
        maxInquiriesPerProject: 3,
        replyDeadlineHours: 24,
        maxAttachments: 5,
        maxAttachmentSize: 10
      },
      smsSettings: {
        enableSms: true,
        enableVoice: true,
        smsRetryCount: 3,
        voiceRetryCount: 2,
        retryInterval: 5
      }
    }
  },
  mounted() {
    this.fetchSettings()
  },
  methods: {
    async saveInquirySettings() {
      if (!this.$refs.inquiryForm || !this.$refs.inquiryForm.validate()) return
      this.savingInquiry = true
      try {
        // 这里可替换为真实接口调用
        // await api.updateInquirySettings(this.inquirySettings)
        localStorage.setItem('inquirySettings', JSON.stringify(this.inquirySettings))
        this.$emit('saved', 'inquiry')
      } catch (error) {
        console.error('保存询标设置失败:', error)
      } finally {
        this.savingInquiry = false
      }
    },
    async saveSmsSettings() {
      if (!this.$refs.smsForm || !this.$refs.smsForm.validate()) return
      this.savingSms = true
      try {
        // 这里可替换为真实接口调用
        // await api.updateSmsSettings(this.smsSettings)
        localStorage.setItem('smsSettings', JSON.stringify(this.smsSettings))
        this.$emit('saved', 'sms')
      } catch (error) {
        console.error('保存短信设置失败:', error)
      } finally {
        this.savingSms = false
      }
    },
    async fetchSettings() {
      try {
        // 可替换为真实接口调用
        const cachedInquiry = localStorage.getItem('inquirySettings')
        const cachedSms = localStorage.getItem('smsSettings')
        if (cachedInquiry) this.inquirySettings = JSON.parse(cachedInquiry)
        if (cachedSms) this.smsSettings = JSON.parse(cachedSms)
      } catch (error) {
        console.error('获取设置失败:', error)
      }
    }
  }
}
</script>

<style scoped>
.parameters-settings {
  padding: 24px 0;
}
</style>